<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title></title>
</head>
<body>
    <div id="root"> </div>

    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>

    <script type="text/babel">
        class App extends React.Component{
            //1.设置状态
            state = {
                username: '',
                password: ''
            }

            render(){
                return <div>
                        <h2>用户登录</h2>
                        <form action="">
                            {/** 2. input 设置 value 属性  添加 onChange 事件 on 当...时候  change 改变  当 input 的值发生改变会触发 */}
                            用户名: <input type="text"  value={this.state.username} onChange={this.saveUsername} /><br/>
                            密码: <input type="password" value={this.state.password} onChange={this.savePassword} /><br />
                            <input type="submit" value="登录" onClick={this.login} />
                            <button onClick={this.clear}>清空</button>
                        </form>
                    </div>
            }

            //保存用户名的值
            saveUsername = (e) => {
                let v = e.target.value;
                //声明正则
                let reg = /^\d*$/;
                //判断
                if(reg.test(v)){
                    //修改状态
                    //3. 修改 state 状态的值
                    this.setState({
                        username: e.target.value
                    })
                }
            }

            //保存密码的值
            savePassword = (e) => {
                this.setState({
                    password: e.target.value
                })
            }

            login = (e) => {
                //阻止默认行为  建议
                e.preventDefault();

                //输出用户名和密码
                console.log('用户名为: ' + this.state.username, '密码为' + this.state.password);
            }

            clear = (e) => {
                e.preventDefault();
                this.setState({
                    username: '',
                    password: ''
                });
            }
        }

        ReactDOM.render(<App />, document.querySelector("#root"));
    </script>
</body>
</html>